package com.ty.service.impl;

import java.util.*;

import com.ty.dao.MajorDao;
import com.ty.dao.StudentDao;
import com.ty.dao.impl.MajorDaoImpl;
import com.ty.dao.impl.StudentDaoImpl;
import com.ty.entity.Student;
import com.ty.service.StudentService;

public class StudentServiceImpl implements StudentService {
    private final StudentDao studentDao = new StudentDaoImpl();
    private final MajorDao majorDao = new MajorDaoImpl();

    @Override
    public void addStudent(String name, int age, String majorName, String phone, String studentId) {
        // 参数校验
        if (studentId == null || !studentId.matches("\\d{11}")) {
            throw new IllegalArgumentException("学号必须为11位数字");
        }
        if (age < 17 || age > 25) {
            throw new IllegalArgumentException("年龄必须在17-25岁之间");
        }
        if (phone != null && !phone.matches("\\d{11}")) {
            throw new IllegalArgumentException("电话号码必须为11位数字");
        }
        Map<String, Object> major = majorDao.getMajorByName(majorName);
        if (major == null) {
            throw new RuntimeException("专业不存在：" + majorName);
        }
        int majorId = ((Number) major.get("id")).intValue();
        Student student = new Student(0, name, age, majorId, phone, studentId);
        studentDao.addStudent(student);
    }

    @Override
    public void deleteStudent(int id) {
        studentDao.deleteStudent(id);
    }

    @Override
    public void updateStudent(int id, String name, int age, String majorName, String phone, String studentId) {
        Map<String, Object> major = majorDao.getMajorByName(majorName);
        if (major == null) {
            throw new RuntimeException("专业不存在：" + majorName);
        }
        int majorId = ((Number) major.get("id")).intValue();
        Student student = new Student(id, name, age, majorId, phone, studentId);
        studentDao.updateStudent(student);
    }

    @Override
    public List<Map<String, Object>> getAllStudentsWithMajor() {
        List<Map<String, Object>> students = studentDao.getAllStudents();
        return enrichStudentsWithMajor(students);
    }

    @Override
    public List<Map<String, Object>> getStudentsByNameWithMajor(String name) {
        List<Map<String, Object>> students = studentDao.getStudentsByName(name);
        return enrichStudentsWithMajor(students);
    }

    private List<Map<String, Object>> enrichStudentsWithMajor(List<Map<String, Object>> students) {
        for (Map<String, Object> student : students) {
            Object majorId = student.get("major_id");
            if (majorId != null) {
                Map<String, Object> major = majorDao.getMajorById(
                    majorId instanceof Number ? ((Number) majorId).intValue() : (Integer) majorId
                );
                if (major != null) {
                    student.put("major_name", major.get("name"));
                } else {
                    student.put("major_name", "未知专业");
                }
            } else {
                student.put("major_name", "未知专业");
            }
        }
        return students;
    }
}